﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace BackgrounderService
{
    class PluginManger
    {
        public PluginManger()
        {
            Scan();
        }

        public void Scan()
        {
            DirectoryInfo dir = new DirectoryInfo("BackgroudAddons");

            if (!dir.Exists)
            {
                dir.Create();
                return;
            }

            foreach (var File in dir.GetFiles("*.dll"))
            {
                try
                {
                    Assembly plugin = Assembly.LoadFile(File.FullName);
                    //System.Windows.Forms.MessageBox.Show(File.FullName);
                    if (plugin != null)
                    {
                        foreach (var t in plugin.GetTypes())
                        {
                            if (t.GetInterfaces().Contains(typeof(IPlugin)))
                            {
                                IPlugin pg = (IPlugin)Activator.CreateInstance(t);
                                pg.Initialization(hp);
                                plugins.Add(pg.Name, pg);
                                var S = pg.GetSettingCallBack();
                                if (S != null)
                                {
                                    settings.Add(S.Value.Key, S.Value.Value);
                                }
                            }
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
               
            }

        }

        public void PluginStateChange(string PluginName,bool Start)
        {
            if (!plugins.ContainsKey(PluginName))
                return;
            var Plugin = plugins[PluginName];
            if (Start)
                Plugin.Start();
            else
                Plugin.Stop();
        }
        public void Setting(string Name)
        {
            if(!settings.ContainsKey(Name))
                return;
            var Del = settings[Name];
            if (Del != null)
                Del.BeginInvoke(null, null);
        }
        public Dictionary<string, bool> GetPlugins()
        {
            var dir =  new Dictionary<string ,bool>();
            foreach (KeyValuePair<string,IPlugin> p in plugins)
            {
                dir.Add(p.Key, p.Value.state == State.Start);
            }
            return dir;
        }
        public List<string> GetSettings()
        {
            var dir = new List<string>();
            foreach (string k in settings.Keys)
            {
                dir.Add(k);
            }
            return dir;
        }

        private Dictionary<string, SettingHandler> settings = new Dictionary<string, SettingHandler>();

        private Dictionary<string, IPlugin> plugins = new Dictionary<string, IPlugin>();

        private HookProxy hp = new HookProxy();



    }
}
